package cn.iocoder.yudao.module.hs.service.qchistory;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.hs.controller.admin.shiporder.vo.ShipOrderRespVO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.hs.controller.admin.qchistory.vo.*;
import cn.iocoder.yudao.module.hs.dal.dataobject.qchistory.QcHistoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.hs.dal.mysql.qchistory.QcHistoryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.*;

/**
 * 物料质检历史记录 Service 实现类
 *
 * @author 航盛
 */
@Service
@Validated
public class QcHistoryServiceImpl implements QcHistoryService {

    @Resource
    private QcHistoryMapper qcHistoryMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createQcHistory(QcHistorySaveReqVO createReqVO) {
        // 插入
        QcHistoryDO qcHistory = BeanUtils.toBean(createReqVO, QcHistoryDO.class);
        qcHistoryMapper.insert(qcHistory);
        // 返回
        return qcHistory.getId();
    }

    @Override
    public void updateQcHistory(QcHistorySaveReqVO updateReqVO) {
        // 校验存在
        validateQcHistoryExists(updateReqVO.getId());
        // 更新
        QcHistoryDO updateObj = BeanUtils.toBean(updateReqVO, QcHistoryDO.class);
        qcHistoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteQcHistory(Long id) {
        // 校验存在
        validateQcHistoryExists(id);
        // 删除
        qcHistoryMapper.deleteById(id);
    }

    private void validateQcHistoryExists(Long id) {
        if (qcHistoryMapper.selectById(id) == null) {
            throw exception(QC_HISTORY_NOT_EXISTS);
        }
    }

    @Override
    public QcHistoryDO getQcHistory(Long id) {
        return qcHistoryMapper.selectById(id);
    }

    @Override
    public PageResult<QcHistoryRespVO> getQcHistoryPage(QcHistoryPageReqVO pageReqVO) {
        PageResult<QcHistoryDO> qcHistoryDOPageResult = qcHistoryMapper.selectPage(pageReqVO);
        if (CollUtil.isEmpty(qcHistoryDOPageResult.getList())) {
            return PageResult.empty();
        }
        List<AdminUserRespDTO> userList = adminUserApi.getUserList();
        // 获取用户列表并构建 userId -> nickname 的映射
        Map<Long, String> userNicknameMap = userList.stream()
                .collect(Collectors.toMap(AdminUserRespDTO::getId, AdminUserRespDTO::getNickname));

        PageResult<QcHistoryRespVO> qcHistoryRespVOPageResult = BeanUtils.toBean(qcHistoryDOPageResult, QcHistoryRespVO.class);
        // 遍历 qcHistoryRespVOPageResult.getList() 并赋值 creatorName
        qcHistoryRespVOPageResult.getList().forEach(qcHistoryRespVO -> {
            // 匹配 creator 对应的 nickname
            if (qcHistoryRespVO.getOperator() != null && userNicknameMap.containsKey(Long.valueOf(qcHistoryRespVO.getOperator()))) {
                qcHistoryRespVO.setOperatorName(userNicknameMap.get(Long.valueOf(qcHistoryRespVO.getOperator())));
            }
        });
        return qcHistoryRespVOPageResult;
    }

}